<?php
namespace app\admin\model;

use think\Model;
use think\Db;

class StudentModel extends Model{
	// 确定链接表名
    protected $name = 'student';

	/**
	 * [获取信息]
	 */
	public function getInfoByWhere($where='',$whereName='',$wherePhone='',$whereSchool='',$whereMajor='',$whereBatch='',$whereClass='',$whereSchoolRoll='',$whereRollStatus='',$is_page=false,$offset=0,$limit=20,$order='id desc'){
 		if($is_page){
 			return $this->where($where)
				->where($whereName)
				->where($wherePhone)
				->where($whereSchool)
				->where($whereMajor)
				->where($whereBatch)
				->where($whereClass)
				->where($whereSchoolRoll)
				->where($whereRollStatus)
				->where($this->whereSchool())->limit($offset, $limit)->order($order)->select();
 		}else{
 			return $this->where($where)
				->where($whereName)
				->where($wherePhone)
				->where($whereSchool)
				->where($whereMajor)
				->where($whereBatch)
				->where($whereClass)
				->where($whereSchoolRoll)
				->where($whereRollStatus)
				->where($this->whereSchool())->count();
 		}
 	}

	/**
	 * [获取所有学生信息]
	 */
	public function getAllStInfoNoPage(){
		return $this->where('delete_status',0)->where($this->whereSchool())->select();
	}

	/**
	 * [编辑学员信息]
	 */
	public function editSTinfo($param){
		try{
			$result = $this->validate('STValidate')->isUpdate(true)->allowField(true)->save($param);
            if(false === $result){
                return ['code'=>0,'msg'=>'编辑失败:'.$this->getError()];
            }else{
                return ['code'=>1,'msg'=>'编辑成功'];
            }
			return ['code'=>1,'msg'=>'编辑成功'];
		}catch(Exception $e){
			return ['code'=>0,'msg'=>'编辑失败:'.$e->getMessage()];
		}
	}

	/**
	 * [安排考试时间]
	 */
	public function editTestTime($idArr,$test_time){
		$this::startTrans();
		try{
			foreach($idArr as $v){
				$this->where('id',$v)->update(['test_time'=>$test_time]);
			}
			$this->commit();
			return ['code'=>1,'msg'=>'考试时间安排成功'];
		}catch(\Exception $e){
			$this->rollback();
			return ['code'=>0,'msg'=>'考试时间安排失败:'.$e->getMessage()];
		}
	}

	/**
	 * [伪删除学员]
	 */
	public function delST($idArr){
		$this::startTrans();
        try{
            foreach($idArr as $v){
                $this->where('id',$v)->update(['delete_status'=>1]);
            }
            $this::commit();
            return ['code'=>1,'msg'=>'删除成功，已放入回收站'];
        }catch(Exception $e){
            $this::rollback();
            return ['code'=>0,'msg'=>"删除失败：".$e->getMessage()];
        }
	}

	/**
     * [删除学员(删除)]
     */
    public function STDelete($idArr){
        $this::startTrans();
        try{
            foreach($idArr as $v){
                $this->where('id',$v)->delete();
            }
            $this::commit();
            return ['code'=>1,'msg'=>'删除成功'];
        }catch(Exception $e){
            $this::rollback();
            return ['code'=>0,'msg'=>"删除失败：".$e->getMessage()];
        }
    }

	/**
     * [恢复学员]
     */
    public function STRecovery($idArr){
        $this::startTrans();
        try{
            foreach($idArr as $v){
                $this->where('id',$v)->update(['delete_status'=>0]);
            }
            $this::commit();
            return ['code'=>1,'msg'=>'恢复成功'];
        }catch(Exception $e){
            $this::rollback();
            return ['code'=>0,'msg'=>"恢复失败：".$e->getMessage()];
        }
    }

	/**
	 * [教务老师只能看见自己所管理的学校的学生]
	 */
	public function whereSchool(){
		$school_id = DB::name('teacher')->where('user_id',session('id'))->value('school_id');
		if($school_id == ''){
			// 如果学校ID为空则证明登录者不是教务老师，则能看见所有学员
			return null;
		}
		$idArr = explode(",",$school_id);
		$this->where(function($query)use($idArr){
            foreach($idArr as $v){
                $query->whereOr('school_id',$v);
            }
        });
	}
}
